<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <!-- 账号输入框 -->
                <el-form-item label="仓库名称" prop="title">
                    <el-input
                        v-model="formData.title"
                        :disabled="formData.root == 1"
                        placeholder="请输入仓库名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="仓库类型">
                    <el-select
                        class="flex-1"
                        v-model.number="formData.type"
                        placeholder="请选择仓库类型"
                    >
                        <el-option
                            v-for="(item, index) in options.jobs"
                            :key="index"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <div>
                        <el-input-number v-model="formData.sort" />
                        <div class="form-tips">数值越小越排前</div>
                    </div>
                </el-form-item>
                <el-form-item label="仓库状态" v-if="formData.root != 1">
                    <el-switch v-model="formData.status" :active-value="0" :inactive-value="1" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input
                        v-model="formData.remark"
                        :disabled="formData.root == 1"
                        placeholder="请输入备注"
                    ></el-input>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { roleLists } from '@/api/perms/role'
import { adminAdd, adminEdit } from '@/api/warehouse/list'
import { deptLists } from '@/api/org/department'
import { jobsLists } from '@/api/org/post'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑仓库' : '新增仓库'
})
const formData = reactive({
    id: '',
    title: '',
    type: '',
    status: '',
    remark: '',
    sort: ''
})

const formRules = reactive({
    title: [
        {
            required: true,
            message: '请输入仓库名称',
            trigger: ['blur']
        }
    ]
})
const options = reactive({
    role: [] as any[],
    dept: [] as any[],
    jobs: [] as any[]
})

const getOptions = () => {
    roleLists({
        page_type: 0
    }).then((data: any) => {
        options.role = data.lists
    })
    deptLists({
        page_type: 0
    }).then((data: any) => {
        options.dept = data
    })
    jobsLists({
        page_type: 0
    }).then((data: any) => {
        options.jobs = data.lists
    })
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            if (data.type) {
                data.type = Number(data.type)
            }
            formData[key] = data[key]
        }
    }
}

const handleClose = () => {
    emit('close')
}

getOptions()

defineExpose({
    open,
    setFormData
})
</script>
